/**********************************************************************
 *** *                                                            * ***
 *** *  Copyright (c) 2013 Harmonia Holdings Group LLC            * ***
 *** *                                                            * ***
 *** * Permission is hereby granted to use, modify, copy, and     * ***
 *** * distribute this software and its documentation for any     * ***
 *** * purpose only without profit, provided that the above       * ***
 *** * Copyright Notice appears in all copies and that both the   * ***
 *** * Copyright Notice and this Permission Notice appears in     * ***
 *** * every copy of supporting documentation.  No title to nor   * ***
 *** * ownership of the software is transferred hereby.  The name * ***
 *** * of Harmonia Holdings Group LLC shall not be used in        * ***
 *** * advertising or publicity related to the distribution of    * ***
 *** * the software without specific, written, prior permission.  * ***
 *** * This software is provided as-delivered without expressed   * ***
 *** * or implied warranty.  Harmonia Holdings Group LLC          * ***
 *** * makes no representation about the suitability of this      * ***
 *** * software for any purpose and accepts no responsibility for * ***
 *** * its use.                                                   * ***
 *** *                                                            * ***
 *** ************************************************************** ***
 *** *                                                            * ***
 *** * This program is free software; you can redistribute it     * ***
 *** * and/or modify it under the terms of the GNU General Public * ***
 *** * License as published by the Free Software Foundation;      * ***
 *** * either version 2 of the License, or (at your option) any   * ***
 *** * later version.                                             * ***
 *** *                                                            * ***
 *** * This program is distributed in the hope that it will be    * ***
 *** * useful, but WITHOUT ANY WARRANTY; without even the implied * ***
 *** * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR    * ***
 *** * PURPOSE.  See the GNU General Public License for more      * ***
 *** * details.                                                   * ***
 *** *                                                            * ***
 *** * You should have received a copy of the GNU General Public  * ***
 *** * License along with this program; if not, write to the Free * ***
 *** * Software Foundation, Inc., 51 Franklin Street, Fifth       * ***
 *** * Floor, Boston, MA 02110-1301, USA.                         * ***
 *** *                                                            * ***
 *** * For more information: http://www.gnu.org/licenses/gpl.html * ***
 *** *                                                            * ***
 **********************************************************************/
package org.etexascode.interrep.datamodel.utils;

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * Generic JAXB reader- reads generic Manager from xml file
 * @author bmauldon
 *
 */
public class JaxbRead{

    /** Static logger       */
    private static final Logger LOGGER=LoggerFactory.getLogger(JaxbRead.class);
    /**
     * Reads an object of the given class from the xml file with the given name on this class's classpath.
     * @param xmlfile The name of the file to read from (on this class's classpath).
     * @param clazz The class being read in.
     * @return manager The object represented by the xml file.
     */
    @SuppressWarnings("unchecked")
    public static <T> T readJaxManager(String xmlfile , Class clazz){

	T manager=null;

	try {
	    JAXBContext jc= JAXBContext.newInstance(clazz);
	    Unmarshaller u= jc.createUnmarshaller();
	    File resource = FileUtils.toFile(JaxbRead.class.getClass().getResource(xmlfile));
	    
	    if (resource == null) {
	    	LOGGER.error("File was null during unmarshalling.");
	    }

	    manager =  (T) u.unmarshal(resource);


	} catch (JAXBException ex) {
	    LOGGER.debug("JAXB Exception", ex);
	}
	return manager;

    }

    /**
     * Reads an object of the given class from the given xml File.
     * @param xmlfile The file to read from.
     * @param clazz The class being read in.
     * @return The object represented by the xml file.
     */
    public static <T> T readJaxManager(File xmlfile, Class clazz) {
    	T manager = null;
    	
    	try {
    		JAXBContext jc = JAXBContext.newInstance(clazz);
    		Unmarshaller u = jc.createUnmarshaller();
    		manager = (T) u.unmarshal(xmlfile);
    	}
    	catch (JAXBException ex) {
    		LOGGER.debug("JAXB Exception", ex);
    	}
    	return manager;
    }

}
